Exercise: Instead of returning a FeatureCollection of sampledPoints around each buffered point, modify the samplePoints function so that it:
Takes the mean of all the samplePoints within a bufferzone using the same “reduceColumns” method I showed you earlier.
Creates a feature with a null geometry and the bufferzone mean assigned to the buffered point ID from which the points were sampled.
Uses this new function and a mapping operation to create the meanPopDensFC variable without needing to compute sampledPoints, meanPopDens, or meanPopDensList.
time:30 minutes
// Function to sample random points within each buffered area
var samplePoints = function (feature) {
var geometry = feature.geometry();
var randomPoints = ee.FeatureCollection.randomPoints(geometry, 10);
var sampledPoints = popDensYearBuffered.sampleRegions({
collection: randomPoints,
scale: 1000,
tileScale: 16
})
// Add the buffered area ID to each sampled point
sampledPoints = sampledPoints.map(function(point) {
return point.set('BufferedAreaID', feature.get('ID'));
});
// Calculate the mean 'population_density' for each group
var meanPopDens = sampledPoints.reduceColumns({
reducer: ee.Reducer.mean(),
selectors: ['population_density']
});
return ee.Feature(null, {
'BufferedAreaID': feature.get('ID'),
'mean': meanPopDens.get('mean')
});
};
// Sample 10 random points within each buffered area and calculate the mean population density
var meanPopDensFC = bufferedPoints.map(samplePoints);Another way to simplify might be to create a new module to store functions we have created in this script.
time: 10 minutes
To include this module we will add the following line to the beginning of our port finding script:
Exercise:
time: 20 minutes